import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutterApp/api/Api.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import '../common/Utils.dart';

class ValvePage extends StatefulWidget {
  const ValvePage({Key key}) : super(key: key);

  @override
  _ValvePageState createState() => _ValvePageState();
}

class _ValvePageState extends State<ValvePage> {
  List _list = [];
  void initState() {
    // TODO: implement initState
    super.initState();
    this.getData();
  }

  void getData() async {
    //showOkAlertDialog(context:context,title:"登录成功");
    Response response = await getAlarmList();
    Map<String, dynamic> data = response.data;
    if (!data['isCorrect']) return;
    setState(() {
      _list = data['data']['data']['recordArray'];
      print(_list);
    });
  }

  void getOpeStatus() async {
    Response response = await getStatus2();
    Map<String, dynamic> data = response.data;
    if (!data['isCorrect']) return;
    setState(() {
      EasyLoading.showToast('控制成功');
    });
  }

  List<Widget> getListData() {
    var result = _list.map((item) {
      return Column(children: [
        Padding(
          padding: EdgeInsets.fromLTRB(0, 6, 0, 6),
          child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Text('阀门状态',
                          style: TextStyle(color: Colors.grey, height: 1.3))
                    ]),
                Text(getValueDes(item['valveStatus']),
                    style: TextStyle(height: 1.2, color: Colors.red))
              ]),
        ),
        Padding(
          padding: EdgeInsets.fromLTRB(0, 6, 0, 6),
          child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Text('上报时间',
                          style: TextStyle(color: Colors.grey, height: 1.3))
                    ]),
                Text(formateByTime(item['recordTime']),
                    style: TextStyle(height: 1.2))
              ]),
        ),
        Divider()
      ]);
    });
    return result.toList();
  }

  Widget _createPage(context) {
    return Container(
        width: double.infinity,
        child: Stack(overflow: Overflow.visible, children: [
          Positioned(
            child: Image.asset("images/bg.png"),
          ),
          Positioned(
            top: 40,
            width: MediaQuery.of(context).size.width,
            child: Container(
              padding: EdgeInsets.fromLTRB(30, 0, 30, 0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  InkWell(
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Image.asset("images/x-valve-0.png", width: 35),
                          SizedBox(height: 5),
                          Text("解除强制关阀", style: TextStyle(color: Colors.white)),
                        ]),
                    onTap: () {
                      this.getOpeStatus();
                    },
                  ),
                  InkWell(
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Image.asset("images/x-valve-1.png", width: 35),
                          SizedBox(height: 5),
                          Text("强制关阀", style: TextStyle(color: Colors.white)),
                        ]),
                    onTap: () {
                      this.getOpeStatus();
                    },
                  ),
                  InkWell(
                    child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Image.asset("images/x-valve-2.png", width: 35),
                          SizedBox(height: 5),
                          Text("预关阀", style: TextStyle(color: Colors.white)),
                        ]),
                    onTap: () {
                      this.getOpeStatus();
                    },
                  )
                ],
              ),
            ),
          ),
          Positioned(
            top: 140,
            left: 10,
            right: 10,
            child: Column(children: [
              Container(
                constraints: BoxConstraints(minHeight: 300.0),
                padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.elliptical(5, 5)),
                  boxShadow: [
                    BoxShadow(
                        color: Colors.grey[200],
                        blurRadius: 2.0,
                        spreadRadius: 2.0),
                  ],
                ),
                child: Column(children: [
                  Row(
                    children: [
                      Image.asset("images/alarm-icon.png", width: 13),
                      SizedBox(width: 5),
                      Text(
                        "报警信息",
                        style: TextStyle(color: Colors.blue),
                      )
                    ],
                  ),
                  SizedBox(height: 15),
                  Column(children: this.getListData())
                ]),
              )
            ]),
          )
        ]));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("阀门控制"),
          centerTitle: true,
          // backgroundColor:Color(0xFF000000)
        ),
        body: SingleChildScrollView(child: this._createPage(context)));
  }
}
